package com.yima.baseproj.module.sys.student;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.read.listener.PageReadListener;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yima.baseproj.config.exception.type.ApiException;
import com.yima.baseproj.module.sys.clazz.ClassEntity;
import com.yima.baseproj.module.sys.clazz.ClassService;
import com.yima.baseproj.module.sys.kindergarten.KindergartenEntity;
import com.yima.baseproj.module.sys.kindergarten.KindergartenService;
import com.yima.baseproj.module.sys.student.model.StudentExcel;
import com.yima.baseproj.module.sys.student.model.StudentSelectModel;
import com.yima.baseproj.module.sys.user.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.Map;

@Service
public class StudentService extends ServiceImpl<StudentMapper, StudentEntity> {

    @Autowired
    private SysUserService userService;

    @Autowired
    private ClassService classService;

    @Autowired
    private KindergartenService kgService;

    /**
     * 分页+模糊查询
     * @param page
     * @param searchModel
     * @return
     */
    public Page<Map> findAllByPage(Page page, StudentEntity searchModel) {
        int kgId = userService.findKgIdByUserId();
        if(kgId==0){
            return baseMapper.findBySearchVo(page,searchModel);
        }else {
            return baseMapper.findByKgId(kgId,page,searchModel);
        }

    }

    public List<Map> findAllByList(StudentEntity searchModel) {
        List<Map> studentList = baseMapper.findBySearchVo(searchModel);
        return studentList;
    }

    /**
     * 根据班级id查找学生列表
     * @param classId
     * @return
     */
    public List<StudentSelectModel> findAllByClassId(int classId) {
        List<StudentSelectModel> studentList =
                baseMapper.findAllByClassId(classId);
        return studentList;
    }

    public List<StudentSelectModel> sysStudentFindBystuName(String stuName) {
        List<StudentSelectModel> studentList =
                baseMapper.sysStudentFindBystuName(stuName);
        return studentList;
    }




    /**
     * 新增操作
     * @param student
     */
    @Transactional(rollbackFor = Exception.class)
    public void add(StudentEntity student) {
        baseMapper.insert(student);
    }

    public StudentEntity findById(int id){
        return  baseMapper.selectById(id);
    }


    @Transactional(rollbackFor = Exception.class)
    public void edit(StudentEntity student){
         baseMapper.updateById(student);
    }

    @Transactional(rollbackFor = Exception.class)
    public void delByIds(int id) {
            baseMapper.deleteById(id);
    }


    /**
     * 获取学生列表, 用于导出
     * @return
     */
    public List<StudentSelectModel> findStuList() {
        //判断当前用户
        int kgId = userService.findKgIdByUserId();
        if(kgId==0){
            return baseMapper.findStuList();
        }else {
            return baseMapper.findStuByKgId(kgId);
        }
    }

    public StudentSelectModel findStudentById(int studentId){
        return baseMapper.findStudentById(studentId);

    }


    public void importStudent(MultipartFile multipartFile) throws IOException {

        EasyExcel.read(multipartFile.getInputStream(), StudentExcel.class, new PageReadListener<StudentExcel>(dataList -> {
            for (StudentExcel student : dataList) {
                //查找幼儿园
                //管理员导入
                int kgId = 0;
                if(userService.findKgIdByUserId()==0){
                    KindergartenEntity kg = kgService.findByName(student.getKgName());
                    if(ObjectUtils.isEmpty(kg)){
                        throw new ApiException("该幼儿园未添加,或幼儿园名称不一致");
                    }
                    kgId = kg.getId();

                }else {
                    //园长导入
                    kgId = userService.findKgIdByUserId();
                }

                //查找班级
                ClassEntity classEntity = classService.findByName(student.getClassName());
                int classId = 0;
                //班级不存在,添加班级
                if(ObjectUtils.isEmpty(classEntity)){
                    ClassEntity cl = new ClassEntity();
                    cl.setClassName(student.getClassName());
                    cl.setKgId(kgId);
                    classService.add(cl);
                    classId = cl.getId();
                }else {
                    classId = classEntity.getId();
                }

                //添加学生
                StudentEntity studentEntity = new StudentEntity();
                studentEntity.setClassId(classId);
                studentEntity.setStudentSex(student.getStudentSex());
                studentEntity.setStudentName(student.getStudentName());
                studentEntity.setStudentBirthday(student.getStudentBirthday());
                this.save(studentEntity);

            }
        })).sheet().doRead();

    }
}
